package com.ai.service.impl;

import com.ai.dao.mapper.BfmUserMapper;
import com.ai.service.LoginLogService;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.collections4.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.*;

@Service
public class LoginLogServiceImpl implements LoginLogService {

    @Autowired
    private BfmUserMapper bfmUserMapper;

    @Override
    public List<JSONObject> getLoginLogs(SimpleDateFormat simpleDateFormat, String now) {
        List<Map<String, Object>> loginLogs = bfmUserMapper.queryLoginLogs();
        List<JSONObject> jsonObjects = new ArrayList<>();
        for (Map<String, Object> loginLogMap : loginLogs) {
            JSONObject jsonObject = new JSONObject();   // 登录日志信息
            JSONObject jsonObject2 = new JSONObject();  // 日志通用字段
            JSONObject jsonObject3 = new JSONObject();  // 汇总JSON
            // 登录日志信息
            jsonObject.put("logId", MapUtils.getString(loginLogMap, "log_id"));
            jsonObject.put("clientIp", MapUtils.getString(loginLogMap, "src_ip"));
            jsonObject.put("clientMac", "");
            jsonObject.put("clientType", "WEB");
            jsonObject.put("clientDeviceId", "");
            jsonObject.put("account", MapUtils.getString(loginLogMap, "party_code"));
            jsonObject.put("phone", MapUtils.getString(loginLogMap, "phone"));
            jsonObject.put("unit", "【天翼云】大数据和AI中心");
            jsonObject.put("unitId", "8500066");
            jsonObject.put("department", "");
            jsonObject.put("position", "");
            jsonObject.put("businessIp", "127.0.0.1");
            jsonObject.put("businessSystemName", "【天翼云】中国电信重庆公司信息服务业务系统AI通用平台");
            jsonObject.put("businessSystemId", "10069");
            if ("login".equals(MapUtils.getString(loginLogMap, "event_type"))) {
                jsonObject.put("loginType", "1");
            } else if ("logout".equals(MapUtils.getString(loginLogMap, "event_type"))) {
                jsonObject.put("loginType", "0");
            }
            jsonObject.put("result", "1");
            jsonObject.put("loginTime", simpleDateFormat.format(loginLogMap.get("log_date")));
            jsonObject.put("loginDesc", MapUtils.getString(loginLogMap, "comments"));
            jsonObject.put("AuthenticatMode", "2");
            jsonObject.put("clientBrand", "");
            jsonObject.put("clientName", "");
            jsonObject.put("clientSN", "");
            // 日志通用字段
            jsonObject2.put("log_province", "85");
            jsonObject2.put("logTime", simpleDateFormat.format(loginLogMap.get("log_date")));
            jsonObject2.put("logID", MapUtils.getString(loginLogMap, "log_id"));
            jsonObject2.put("Log_type", "025");
            jsonObject2.put("equ_manuf", "");
            jsonObject2.put("dev_mod", "");
            jsonObject2.put("devIPv4", "");
            jsonObject2.put("devIPv6", "");
            jsonObject2.put("devMAC", "");
            jsonObject2.put("device_id", "");
            jsonObject3.put("busiAnnotations", jsonObject);
            jsonObject3.put("commonAnnotations", jsonObject2);
            // 汇总JSON
            jsonObjects.add(jsonObject3);
        }
        return jsonObjects;
    }

    @Override
    public List<JSONObject> getQueryLogs(SimpleDateFormat simpleDateFormat, String now) {
        Random random = new Random();
        int view = random.nextInt(8) + 1;
        List<Map<String, Object>> operationLogs = bfmUserMapper.queryLoginLogs();
        List<JSONObject> jsonObjects = new ArrayList<>();
        for (Map<String, Object> operationLogMap : operationLogs) {
            JSONObject jsonObject = new JSONObject();   // 操作日志信息
            JSONObject jsonObject2 = new JSONObject();  // 日志通用字段
            JSONObject jsonObject3 = new JSONObject();  // 汇总JSON
            String id = MapUtils.getString(operationLogMap, "log_id");
            jsonObject.put("logId", id);
            jsonObject.put("clientIp", "");
            jsonObject.put("clientMac", "");
            jsonObject.put("clientType", "WEB");
            jsonObject.put("clientDeviceId", "");
            jsonObject.put("account", MapUtils.getString(operationLogMap, "user_code"));
            jsonObject.put("phone", MapUtils.getString(operationLogMap, "phone"));
            jsonObject.put("unit", "【天翼云】大数据和AI中心");
            jsonObject.put("unitId", "8500066");
            jsonObject.put("department", "");
            jsonObject.put("position", "");
            jsonObject.put("businessIp", "127.0.0.1");
            jsonObject.put("businessSystemName", "【天翼云】中国电信重庆公司信息服务业务系统AI通用平台");
            jsonObject.put("businessSystemId", "10069");
            jsonObject.put("operate_type", "4");
            if ("1".equals(String.valueOf(view))) {
                jsonObject.put("operate_business", "用户管理");
                jsonObject.put("operate_desc", "【天翼云】查询用户管理信息成功");
                jsonObject.put("operate_command", "SELECT * FROM BFM_USER");
            } else if ("2".equals(String.valueOf(view))) {
                jsonObject.put("operate_business", "能力管理");
                jsonObject.put("operate_desc", "【天翼云】查询能力管理信息成功");
                jsonObject.put("operate_command", "SELECT * FROM air_service where create_user = " + MapUtils.getString(operationLogMap, "user_code"));
            } else if ("3".equals(String.valueOf(view))) {
                jsonObject.put("operate_business", "notebook");
                jsonObject.put("operate_desc", "【天翼云】查询notebook信息成功");
                jsonObject.put("operate_command", "SELECT * FROM air_notebook where create_user = " + MapUtils.getString(operationLogMap, "user_code"));
            } else if ("4".equals(String.valueOf(view))) {
                jsonObject.put("operate_business", "标注任务");
                jsonObject.put("operate_desc", "【天翼云】查询标注任务信息成功");
                jsonObject.put("operate_command", "SELECT * FROM tai_mark_task where create_user = " + MapUtils.getString(operationLogMap, "user_code"));
            } else if ("5".equals(String.valueOf(view))) {
                jsonObject.put("operate_business", "镜像管理");
                jsonObject.put("operate_desc", "【天翼云】查询镜像管理信息成功");
                jsonObject.put("operate_command", "SELECT * FROM tai_images where create_user = " + MapUtils.getString(operationLogMap, "user_code"));
            } else if ("6".equals(String.valueOf(view))) {
                jsonObject.put("operate_business", "能力部署");
                jsonObject.put("operate_desc", "【天翼云】查询能力部署信息成功");
                jsonObject.put("operate_command", "SELECT * FROM air_service_instance where create_user = " + MapUtils.getString(operationLogMap, "user_code"));
            } else if ("7".equals(String.valueOf(view))) {
                jsonObject.put("operate_business", "资源池");
                jsonObject.put("operate_desc", "【天翼云】查询资源池信息成功");
                jsonObject.put("operate_command", "SELECT * FROM air_resource_pools where create_user = " + MapUtils.getString(operationLogMap, "user_code"));
            } else if ("8".equals(String.valueOf(view))) {
                jsonObject.put("operate_business", "样本库");
                jsonObject.put("operate_desc", "【天翼云】查询样本库信息成功");
                jsonObject.put("operate_command", "SELECT * FROM tai_panels where create_user = " + MapUtils.getString(operationLogMap, "user_code"));
            }

            jsonObject.put("operate_time", now);
            jsonObject.put("result", "1");
            jsonObject.put("operate_param", "");
            jsonObject.put("operate_level", "1");
            jsonObject.put("operate_sensitive", "0");
            jsonObject.put("operate_reason", "0");
            jsonObject.put("operate_reason_desc", "");
            jsonObject.put("operate_vn", "");
            jsonObject.put("SensitiveLevel", "D6-1");
            jsonObject.put("ExportStaff", "");
            jsonObject.put("ExportTime", "");
            jsonObject.put("ExportFileName", "");

            jsonObject2.put("log_province", "85");
            jsonObject2.put("logTime", now);
            jsonObject2.put("logID", id);
            jsonObject2.put("Log_type", "026");
            jsonObject2.put("equ_manuf", "");
            jsonObject2.put("dev_mod", "");
            jsonObject2.put("devIPv4", "");
            jsonObject2.put("devIPv6", "");
            jsonObject2.put("devMAC", "");
            jsonObject2.put("device_id", "");
            jsonObject3.put("busiAnnotations", jsonObject);
            jsonObject3.put("commonAnnotations", jsonObject2);
            jsonObjects.add(jsonObject3);
        }
        return jsonObjects;
    }
}
